package com.bd.scm.module.login.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bd.scm.dao.mapper.login.LoginMapper;
import com.bd.scm.domain.dto.login.LoginDto;
import com.bd.scm.module.login.service.LoginService;
import com.bd.scm.module.utils.EncrypDES;
import com.bd.scm.module.utils.LdapUtils;
import com.bd.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**登录ServiceImpl
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Author YH
 * @Date 2019/12/30 0030 9:19
 * @Version 1.0
 **/
@Service
public class LoginServiceImpl implements LoginService {

  /**
   * 登录Mapper
   */
  @Autowired
  private LoginMapper loginMapper;

  /**
   * 登录验证
   * @param loginDto activeId 1 域登录  2工号登录  username 账号 password 密码
   * @param clientIp  操作人IP
   * @return
   */
  @Override
  public JSONObject valiLogin(LoginDto loginDto, String clientIp, HttpServletRequest request) throws Exception {
    JSONObject rt = new JSONObject();
    if("1".equals(loginDto.getActiveId())){
      //域登录
      //根据邮箱查询是否绑定该邮箱  绑定人数最大为1
      int emailCount = this.loginMapper.getEmailCount(loginDto);
      if(1==emailCount){
        //成功 执行下面操作
        //验证ldap是否成功
        JSONObject jsonObject = LdapUtils.checkLdap(loginDto.getUsername(), loginDto.getPassword());
        if("00000".equals(jsonObject.getString("code"))){
          //验证ldap成功 查询根据邮箱查询工号
          String userNO = this.loginMapper.getUserNO(loginDto);
          if(!"".equals(userNO)){
            //域登录成功
            rt.put("code","00000");
            rt.put("userNO",userNO);
            rt.put("message","成功");
            //设置工号在DTO里
            loginDto.setUsername(userNO);
            this.savaSession(loginDto,request);
          }else{
            //工号查询为空
            rt.put("code","10001");
            rt.put("message","工号查询为空");
          }
        }else{
         return jsonObject;
        }
      }else{
        if(emailCount>1){
          //绑定多人员
          rt.put("code","10006");
          rt.put("message","域绑定多人员");
        }else{
          //未绑定
          rt.put("code","10007");
          rt.put("message","域未绑定");
        }
      }
    }else{
      //工号登录
      //验证工号  密码是否正确
      if("4".equals(loginDto.getActiveId())){
        loginDto.setUsername(new EncrypDES().decrypt(loginDto.getUsername()));
      }
      int userNoCount = this.loginMapper.getUserNoCount(loginDto);
      if(1==userNoCount){
        //工号登录成功
        rt.put("code","00000");
        rt.put("message","成功");
        rt.put("userNO",loginDto.getUsername());
        this.savaSession(loginDto,request);
      }else{
        if(userNoCount>0){
          //工号,密码绑定多人员
          rt.put("code","20001");
          rt.put("message","工号,密码绑定多人员");
        }else{
          //未查询相关登录信息
          rt.put("code","20002");
          rt.put("message","未查到相关登录信息");
        }
      }
    }
    return rt;
  }

  /**
   * 获取登陆人
   * @param loginDto
   * @return
   */
  @Override
  public JSONObject getUser(LoginDto loginDto) {
    return this.loginMapper.getUserDate(loginDto);
  }

  /** 延峰
   * 获取登陆人
   * @param empNo
   * @return
   */
  @Override
  public JSONObject getUser(String empNo) {
    JSONObject user = this.loginMapper.getUser(empNo);
    return user;
  }

  /**
   * 根据ID查询人脸权限
   * @param userSerial
   * @return
   */
  @Override
  public int getUserFace(String userSerial) {
    return this.loginMapper.getUserFace(userSerial);
  }

  /**
   * 保存Session 相关值
   */
  public void savaSession(LoginDto loginDto,HttpServletRequest request){
    JSONObject user = this.loginMapper.getUser(loginDto.getUsername());
    //登录人信息保存到Session
    request.getSession().setAttribute("user",user);
    System.out.println("Session ID:"+request.getSession().getId());
    //查看WT_GLY表是否存在相关数据
    int wtGlyCount = 0;
//    int wtGlyCount = this.loginMapper.findWtGlyCount(loginDto.getUsername());
    if(!StringUtils.isEmpty(user.getString("GroupLname"))){
      wtGlyCount = 1;
    }
    request.getSession().setAttribute("wtGlyCount",wtGlyCount);
    //判断该登录工号是否存在WT_GLY表 不存在 获取登录人部门以及上级部门
    if (wtGlyCount<=0){
        List<String> depSerial =  new ArrayList<String>();
        depSerial.add(user.getString("depSerial"));
//      request.getSession().setAttribute("findDeptid",String.join(",", depSerial));
        request.getSession().setAttribute("findDeptid",depSerial);
    }
  };

}
